<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="大类名称" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大类编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入分类编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['workflow:category:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          class="editButton"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['workflow:category:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['workflow:category:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workflow:category:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="categoryList" @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
      <!-- <el-table-column label="工单大类编号" align="center" prop="categoryId" v-if="true"/> -->
      <el-table-column label="工单大类名称" align="center" prop="categoryName" />
      <el-table-column label="工单大类编码" align="center" prop="code" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workflow:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['workflow:category:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add"
            v-hasPermi="['workflow:category:edit']"
            @click="subSelectClick(scope.row)"
          >子类</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="queryTotal>0"
      :total="queryTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="关联角色" prop="roleIds">
          <el-select v-model="form.roleIds" multiple placeholder="请选择" filterable>
            <el-option
              v-for="item in roleListData"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 工单子类 -->
    <el-dialog title="工单子类管理" :visible.sync="subIslog" width="1200px" append-to-body>
      <div>
        <el-form :model="subQueryParams" ref="subQueryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="子类名称" prop="categoryName">
            <el-input
              v-model="subQueryParams.categoryName"
              placeholder="请输入分类名称"
              clearable
              size="small"
              @keyup.enter.native="querySubClassFUN"
            />
          </el-form-item>
          <el-form-item label="子类编码" prop="code">
            <el-input
              v-model="subQueryParams.code"
              placeholder="请输入分类编码"
              clearable
              size="small"
              @keyup.enter.native="querySubClassFUN"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="querySubClassFUN">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="subReset">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="subClassAdd"
              v-hasPermi="['workflow:category:add']"
            >新增</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="subLoading" :data="subClassData" @selection-change="handleSelectionChange" border>
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
          <!-- <el-table-column label="工单子类编号" align="center" prop="categoryId" v-if="true"/> -->
          <el-table-column label="工单子类名称" align="center" prop="categoryName" />
          <el-table-column label="工单子类编码" align="center" prop="code" />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="subUpdate(scope.row)"
                v-hasPermi="['workflow:category:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="delSubClass(scope.row)"
                v-hasPermi="['workflow:category:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="subTotal>0"
          :total="subTotal"
          :page.sync="subQueryParams.pageNum"
          :limit.sync="subQueryParams.pageSize"
          @pagination="querySubClassFUN"
        />
      </div>
    </el-dialog>
    <!-- 添加或修改【工单子类】对话框 -->
    <el-dialog :title="subTitle" :visible.sync="subOpenAdd" width="500px" append-to-body>
      <el-form ref="subForm" :model="subForm" :rules="subRules" label-width="80px">
        <el-form-item label="子类名称" prop="categoryName">
          <el-input v-model="subForm.categoryName" placeholder="请输入子类名称" />
        </el-form-item>
        <el-form-item label="子类编码" prop="code">
          <el-input v-model="subForm.code" placeholder="请输入子类编码" />
        </el-form-item>
        <el-form-item label="关联角色" prop="roleIds">
          <el-select v-model="subForm.roleIds" multiple placeholder="请选择" filterable>
            <el-option
              v-for="item in roleListData"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
              :disabled="item.status == 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="subForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="subButtonLoading" type="primary" @click="subClassConfirm">确 定</el-button>
        <el-button @click="subCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory,addSubClass,querySubClass,delSubClass,upSubClass } from "@/api/workflow/category";
import { listRole } from "@/api/system/role";

export default {
  name: "Category",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      queryTotal: 0,
      // 【请填写功能名称】表格数据
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,
        code: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        categoryName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "分类编码不能为空", trigger: "blur" }
        ]
      },
      subIslog:false,
      subRow:{},
      // 子类数据
      subClassData:[],
      subLoading:false,
      // 子类查询参数
      subQueryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,
        code: undefined,
      },
      // 新增修改子类参数
      subForm: {
        categoryName:"",
        code:"",
        roleIds: [],
        remark:""
      },
      subTotal:0,
      subRules: {
        categoryName: [
          { required: true, message: "子类名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "子类编码不能为空", trigger: "blur" }
        ]
      },
      subOpenAdd:false,
      subButtonLoading: false,
      subTitle:"工单子类",
      roleListData: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows;
        this.queryTotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        categoryId: undefined,
        categoryName: undefined,
        code: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
        roleIds: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.categoryId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getRoleList()
      this.open = true;
      this.title = "添加模型分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.getRoleList()
      this.reset();
      const categoryId = row.categoryId || this.ids
      getCategory(categoryId).then(response => {
        this.loading = false;
        this.form = response.data;
        if (this.form.roleId && this.form.roleId.length) {
          this.$set(this.form, "roleIds", this.form.roleId.split(','));
        }
        this.open = true;
        this.title = "修改模型分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.roleIds) {
            if (this.form.roleIds.length) {
              this.form.roleId = this.form.roleIds.join()
            } else {
              this.form.roleId = ''
            }
          }
          if (this.form.categoryId != null) {
            updateCategory(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCategory(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const categoryIds = row.categoryId || this.ids;
      this.$modal.confirm('是否确认删除名称为"' + row.categoryName + '"的数据项？').then(() => {
        this.loading = true;
        return delCategory(categoryIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('workflow/category/export', {
        ...this.queryParams
      }, `category_${new Date().getTime()}.xlsx`)
    },
    /** 子类按钮操作 */
    subSelectClick(row){
      this.subQueryParams.categoryName = "";
      this.subQueryParams.code = "";
      this.subIslog = true;
      this.subRow = row;
      this.querySubClassFUN()
      this.getRoleList()
    },
    /** 子类新增 */
    subClassAdd(){
      this.subOpenAdd = true
      this.subTitle = '新增工单子类'
      this.subForm = {
        categoryName:"",
        code:"",
        roleIds: [],
        remark:""
      }
    },
    /** 子类新增修改按钮 */
    subClassConfirm(){
      this.$refs["subForm"].validate(valid => {
        if (valid) {
          this.subButtonLoading = true
          if (this.subForm.roleIds) {
            if (this.subForm.roleIds.length) {
              this.subForm.roleId = this.subForm.roleIds.join()
            } else {
              this.subForm.roleId = ''
            }
          }
          if(this.subTitle == '新增工单子类'){
            // 新增
            let queryParams={
              ...this.subForm,
              firstCategoryId:this.subRow.categoryId
            }
            addSubClass(queryParams).then((res)=>{
              this.$modal.msgSuccess("新增成功");
              this.subOpenAdd = false
              this.querySubClassFUN()
            }).finally(() => {
              this.subButtonLoading = false;
            });
          }else{
            // 修改
            let upParams = {
              ...this.subForm,
            }
            upSubClass(upParams).then(()=>{
              this.$modal.msgSuccess("修改成功");
              this.subOpenAdd = false
              this.querySubClassFUN()
            }).finally(() => {
              this.subButtonLoading = false;
            });
          }
        }
      })
    },
    /** 子类新增取消按钮 */
    subCancel(){
      this.subOpenAdd = false,
      this.subForm = {
        categoryName:"",
        code:"",
        remark:""
      }
    },
    /** 子类查询 */
    querySubClassFUN(){
      let queryForm = {
        ...this.subQueryParams,
        firstCategoryId:this.subRow.categoryId
      }
      this.subLoading = true;
      querySubClass(queryForm).then(response => {
        this.subClassData = response.rows;
        this.subTotal = response.total;
        this.subLoading = false;
      });
    },
    /** 子类修改 */
    subUpdate(row){
      this.subOpenAdd = true
      this.subTitle = '修改工单子类'
      let roleIds = []
      if (row.roleId && row.roleId.length) {
        roleIds = row.roleId.split(',')
      }      
      this.subForm = {
        categoryName:row.categoryName,
        code:row.code,
        roleIds,
        remark:row.remark,
        categoryId:row.categoryId
      }
    },
    delSubClass(row){
      this.$modal.confirm('是否确认删除编号为"' + row.categoryId + '"的数据项？').then(() => {
        return delSubClass(row.categoryId);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.querySubClassFUN()
      }).finally(() => {
        this.loading = false;
      });
    },
    subReset(){
      this.subQueryParams = {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,
        code: undefined,
      };
      this.querySubClassFUN()
    },
    /** 查询角色列表 */
    getRoleList() {
      if (this.roleListData && this.roleListData.length) {
        return
      }
      listRole({
        'pageNum': 1,
        'pageSize': 1000,
      }).then(response => {
          this.roleListData = response.rows;
        }
      );
    },
  }
};
</script>
<style lang="scss" scoped>
.editButton{
  background-color: #fff;
  color: #46a6ff!important;
  border: 1px solid #46a6ff !important;
}
</style>
